﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SonCa.SocialNetwork.Profiles.Presenters;
using SonCa.SocialNetwork.Profiles.IPresenters;
using System.ComponentModel.Composition;
using Core.Interfaces;
using Core.BusinessLogic.Interfaces;
using Core.Common;
using Core.DataAccess;

namespace SonCa.SocialNetwork.Web
{
    public partial class PersonalMaster : System.Web.UI.MasterPage, IUploadAvatar
    {
        private UploadAvatarPresenter _presenter;        

        [Import]
        private IWebContext _webContext;
        [Import]
        private IUserSession _userSession;
        [Import]
        private IUploadService _uploadService;        

        protected void Page_Load(object sender, EventArgs e)
        {
            MEFManager.Compose(this);                        
            _presenter = new UploadAvatarPresenter();
            _presenter.Init(this);

            imgCover.Src = _presenter.DisplayImgCover();
            btnEditAvatar_personal.Visible = true;
            btnEditCover_personal.Visible = true;

            if (_userSession.OtherAccountID > 0)
            {
                btnEditAvatar_personal.Visible = false;
                btnEditCover_personal.Visible = false;
            }
            hdfAccountID_cover.Value = _userSession.CurrentUser.AccountID.ToString();
        }

        protected void btnSubmitEditAvatar_personal_Click(object sender, EventArgs e)
        {
            if (fuAvatarUpload_personal.PostedFile != null || !string.IsNullOrEmpty(fuAvatarUpload_personal.PostedFile.FileName))
            {
                _presenter.UploadFile(fuAvatarUpload_personal.PostedFile, Request.Url.ToString());
            }
        }

        public void ShowMessage(string Message)
        {
            //lblMessage.Text = Message;
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "message", "alert('" + Message + "');", true);
        }

        public void ShowCropPanel()
        {
            pnlUpload.Visible = false;
        }

        public void ShowApprovePanel()
        {
            pnlUpload.Visible = false;
        }

        public void ShowUploadPanel()
        {
            pnlUpload.Visible = true;
        }

        public void ShowAvatar(int accountID)
        {
            imgAvatar_personal.ImageUrl = "~/Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + accountID.ToString();
            imgAvatar.ImageUrl = "~/Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + accountID.ToString();
        }

        public void CloseDialog()
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "close", "closeDialog();", true);
        }

        public void ShowInfo(Account account, WorkPlace workPlace)
        {
            if (account != null)
            {
                lbUsername.Text = account.LastName + " " + account.MidName + " " + account.FirstName;
                lbBirthDate.Text = Convert.ToDateTime(account.BirthDate).ToShortDateString();
            }
            if (workPlace != null)
            {
                lbLive.Text = workPlace.City + ", " + workPlace.Country;
            }
        }
    }
}